package com.ocom.mqsimple.business.forecastData;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.MapKey;
import com.ocom.common.dto.Department;
import com.ocom.common.entity.master.EntityFileDownloadInfo;
import com.ocom.common.request.payconfig.UpdateOrderForecastDetailRequest;
import com.ocom.common.request.simple.StudentExcelRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.RandomUtil;
import com.ocom.common.utils.excel.SXSSFWORKBookUtils;
import com.ocom.common.vo.simple.ListVo;
import com.ocom.common.vo.simple.PersonVo;
import com.ocom.mqsimple.config.ForecastDataMqConfig;
import com.ocom.mqsimple.config.StudentExcelMqConfig;
import com.ocom.mqsimple.feign.AccessClientService;
import com.ocom.mqsimple.feign.HumanClientService;
import com.ocom.mqsimple.feign.MinioClientService;
import com.ocom.mqsimple.feign.PayConfigClientService;
import com.ocom.redis.util.RedisUtil;
import com.rabbitmq.client.Channel;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;


@Component
public class ForecastDataConsumerMqListener {

	private Logger logger = LoggerFactory.getLogger(ForecastDataConsumerMqListener.class);

	@Resource
	private RedisUtil redisUtil;

	@Autowired
	private PayConfigClientService payConfigClientService;
	/**
	 * 把该监听命名为A监听
	 * 监听队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues = ForecastDataMqConfig.FORECAST_DATA_QUEUE)
	public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {

		logger.info("FORECAST_DATA_QUEUE 消费中");
		logger.info("message【{}】",message);
		String requestMessage = new String(message.getBody(), StandardCharsets.UTF_8);
		Object requestRedis = redisUtil.get(requestMessage);

		if (requestRedis == null) {
			logger.info("GzhMsgError:redis为空异常");
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			return;
		}
		//TODO 数据库操作样例
		logger.info("FORECAST_DATA_QUEUE-start:【{}】",requestMessage);
		logger.info("System.currentTimeMillis()【{}】",System.currentTimeMillis());


		UpdateOrderForecastDetailRequest updateOrderForecastDetailRequest= JSON.parseObject(JSON.toJSONString(requestRedis), UpdateOrderForecastDetailRequest.class);
		List<String> dateList = Arrays.asList(StrUtil.split(updateOrderForecastDetailRequest.getDateList(), ","));
		List<String> mealList  = Arrays.asList(StrUtil.split(updateOrderForecastDetailRequest.getMealList(), ","));
		for (int i = 0; i < dateList.size(); i++) {
			for (int j = 0; j < mealList.size(); j++) {
				updateOrderForecastDetailRequest.setMealDate(DateUtil.parseDate(dateList.get(i)));
				updateOrderForecastDetailRequest.setMealNo(mealList.get(j));
				payConfigClientService.updateOrderForecastDetail(updateOrderForecastDetailRequest);
			}
		}
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

	/**
	 * 把该监听命名为B监听
	 * 监听死信队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues =  ForecastDataMqConfig.FORECAST_DATA_DIE_QUEUE)
	public void processForDie(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		//死信队列放弃
		logger.info("FORECAST_DATA_DIE_EXCHANGE 死信队列放弃");

		try {
//			String humanMessage = new String(message.getBody(), StandardCharsets.UTF_8);
//			//TODO 数据库操作样例
//			logger.info("STUDENT_EXCEL_QUEUEE-start:【{}】",humanMessage);
//			logger.info("System.currentTimeMillis()【{}】",System.currentTimeMillis());
//			StudentExcelRequest studentExcelRequest= JSON.parseObject(humanMessage, StudentExcelRequest.class);
//
//			EntityFileDownloadInfo entityFileDownloadInfo=new EntityFileDownloadInfo();
//			entityFileDownloadInfo.setId(studentExcelRequest.getId().longValue());
//			entityFileDownloadInfo.setStatus(2l);
//			entityFileDownloadInfo.setRemark("导出异常-死信队列放弃");
//			accessClientService.UpdateFileStatus(entityFileDownloadInfo);
		}catch (Exception e){
			logger.info("STUDENT_EXCEL_DIE_QUEUE 死信队列处理异常 【{}】",e);
		}


		channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);

	}


}
